using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Keep.Const;
using Keep.Model;
using Keep.Model.Page;
using Keep.Service;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class FoodController : ControllerBase
{

    private readonly IFileService fileService;

    public FoodController(IFileService fileService)
    {
        this.fileService = fileService;
    }
    //博客请求参数类
    public class FoodRequest
    {
        public string? path { get; set; }
        public string? oldname { get; set; }
        public string? newname { get; set; }
        public string? name { get; set; }
        public string? keypath { get; set; }
        public string? content { get; set; }
        public string? selname { get; set; }
        public int currentPage { get; set; }
        public int pageSize { get; set; }
    }


    [HttpPost(Name = "AddFood")]
    public async Task<ActionResult<ResultData>> AddFood(FoodRequest foodRequest)
    {
        Log.Warning("IN => " + foodRequest.path);
        bool sys = await Task.Run(() => fileService.AddFile(foodRequest.path, Static.FoodPath));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "AddFoodDirectory")]
    public async Task<ActionResult<ResultData>> AddFoodDirectory(FoodRequest foodRequest)
    {
        Log.Warning("IN => " + foodRequest.path);
        string sys = await Task.Run(() => fileService.AddFileDirectory(foodRequest.path, Static.FoodPath));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "DelFood")]
    public async Task<ActionResult<ResultData>> DelFood(FoodRequest foodRequest)
    {
        Log.Warning("IN =>   path=" + foodRequest.path + "  name=" + foodRequest.name);
        bool sys = await Task.Run(() => fileService.DelFile(foodRequest.path, Static.FoodPath, foodRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "DelFoodDir")]
    public async Task<ActionResult<ResultData>> DelFoodDir(FoodRequest foodRequest)
    {
        Log.Warning("IN =>   path=" + foodRequest.path + "  name=" + foodRequest.name);
        bool sys = await Task.Run(() => fileService.DelFileDir(foodRequest.path, Static.FoodPath, foodRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }

    [HttpPost(Name = "EditFoodDirOrName")]
    public async Task<ActionResult<ResultData>> EditFoodDirOrName(FoodRequest foodRequest)
    {
        Log.Warning("IN => path=" + foodRequest.path + "    oldname=" + foodRequest.oldname + "    newname=" + foodRequest.newname);
        bool sys = await Task.Run(() => fileService.EditFileDirOrName(foodRequest.path, Static.FoodPath, foodRequest.oldname, foodRequest.newname));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }

    [HttpPost(Name = "GetFoodPathInfo")]
    public async Task<ActionResult<ResultData>> GetFoodPathInfo(FoodRequest foodRequest)
    {
        Log.Warning("IN => " + foodRequest.path);
        PageResult<FileDiy> sys = await Task.Run(() => fileService.GetPathInfo(Static.FoodPath, foodRequest.path, foodRequest.currentPage, foodRequest.pageSize));
        Log.Warning("OUT => " + sys.ToString() + "  数量:" + sys.Rows.Count);
        return Ok(new ResultData(Static.Enable, sys));
    }

    [HttpPost(Name = "GetFoodData")]
    public async Task<ActionResult<ResultData>> GetFoodData(FoodRequest foodRequest)
    {
        Log.Warning("IN => " + foodRequest.keypath);
        string data = await Task.Run(() => fileService.GetFileData(Static.FoodPath, foodRequest.keypath));
        Log.Warning("OUT => " + data);
        return Ok(new ResultData(Static.Enable, data));
    }
    [HttpPost(Name = "SelFoodByName")]
    public async Task<ActionResult<ResultData>> SelFoodByName(FoodRequest foodRequest)
    {
        Log.Warning("IN => " + foodRequest.keypath);
        List<FileDiy> data = await Task.Run(() => fileService.SelFileByName(Static.FoodPath, foodRequest.selname));
        Log.Warning("OUT => " + data);
        return Ok(new ResultData(Static.Enable, data));
    }
    [HttpPost(Name = "SaveFoodData")]
    public async Task<ActionResult<ResultData>> SaveFoodData(FoodRequest foodRequest)
    {
        Log.Warning("IN => " + foodRequest.keypath);
        bool sys = await Task.Run(() => fileService.SaveFileData(Static.FoodPath, foodRequest.keypath, foodRequest.content));
        Log.Warning("OUT => " + sys);
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "UploadFoodImg")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadFoodImg(IFormFile file)
    {
        Log.Warning("UploadFoodImg => IN");
        string sys = fileService.UploadFoodImage(file);
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "UploadFoodThImg")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadFoodThImg(IFormFile file, string? name)
    {
        Log.Warning("UploadFoodThImg => IN");
        if (String.IsNullOrEmpty(name))
        {
            name = Request.Form["name"];
        }
        string sys = fileService.UploadFoodThImage(file, name);
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "UploadFood")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadFood(List<IFormFile> file, string? fileurl)
    {
        try
        {
            if (file.Count > 0)
            {
                if (String.IsNullOrEmpty(fileurl))
                {
                    fileurl = Request.Form["fileurl"];
                }
                Log.Warning("UploadFood => " + fileurl);
                fileService.UploadFile(file, fileurl, Static.FoodPath);
                return Ok(new ResultData(Static.Enable, file[0].FileName));
            }
            else
            {
                return BadRequest("没有发现上传文件");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }
}
